package com.hhx.androidrxmvp.mvp.presenter;

import android.content.Context;

import com.hhx.androidrxmvp.base.mvp.BasePresenter;
import com.hhx.androidrxmvp.bean.BaseResp;
import com.hhx.androidrxmvp.bean.Login;
import com.hhx.androidrxmvp.mvp.contract.LoginContract;
import com.hhx.androidrxmvp.mvp.model.LoginModel;
import com.hhx.androidrxmvp.net.ObserverResponseListener;
import com.hhx.androidrxmvp.net.exception.ApiException;

import java.util.HashMap;

import javax.inject.Inject;

/**
 * Created by Administrator on 2019/3/18.
 */

public class LoginPresenter extends BasePresenter<LoginContract.View> implements LoginContract.Presenter {

    private Context mContext;
    private LoginModel mModel;

    @Inject
    public LoginPresenter(Context mContext) {
        this.mContext = mContext;
        mModel = new LoginModel();
    }

    @Override
    public void login(HashMap<String, String> map, boolean isDialog, boolean cancelable) {
        if (getView() != null) {
            getView().showLoading();
            mModel.login(mContext, map, isDialog, cancelable, getView().<Object>bindToLife(), new ObserverResponseListener() {
                @Override
                public void onNext(Object o) {
                    if (getView() != null) {
                        getView().refreshSuccess((BaseResp<Login>) o);
                        getView().hideLoading();
                    }
                }

                @Override
                public void onError(ApiException e) {
                    if (getView() != null) {
                        getView().refreshFailed(e.getMessage());
                        getView().hideLoading();
                    }
                }
            });
        }
    }

    @Override
    public void loginNormal(HashMap<String, String> map) {
        mModel.loginNormal(mContext, map, getView().<Object>bindToLife(), new ObserverResponseListener() {
            @Override
            public void onNext(Object o) {
                if (getView() != null) {
                    getView().refreshSuccess((BaseResp<Login>) o);
                }
            }

            @Override
            public void onError(ApiException e) {
                if (getView() != null) {
                    getView().refreshFailed(e.getMessage());
                }
            }
        });
    }
}
